Public Function PointInside(ByVal X As Single, ByVal Y As Single) As Boolean
Dim i As Integer
Dim theta1 As Double
Dim theta2 As Double
Dim dtheta As Double
Dim dx As Double
Dim dy As Double
Dim angles As Double
dx = Points(NumPts).trans(1) - X
dy = Points(NumPts).trans(2) - Y
theta1 = ATan2(CSng(dy), CSng(dx))
If theta1 < 0 Then theta1 = theta1 + 2 * PI
For i = 1 To NumPts
dx = Points(i).trans(1) - X
dy = Points(i).trans(2) - Y
theta2 = ATan2(CSng(dy), CSng(dx))
If theta2 < 0 Then theta2 = theta2 + 2 * PI
dtheta = theta2 - theta1
If dtheta > PI Then dtheta = dtheta - 2 * PI
If dtheta < -PI Then dtheta = dtheta + 2 * PI
angles = angles + dtheta
theta1 = theta2
Next i
PointInside = (Abs(angles) > 0.001)
End Function
' Return True if this polygon is completly above
' the plane containing target.
Public Function IsAbove(ByVal target As Face3d) As Boolean
Dim Nx As Single
Dim Ny As Single
Dim Nz As Single
Dim px As Single
Dim py As Single
Dim pz As Single
Dim dx As Single
Dim dy As Single
Dim dz As Single
Dim Cx As Single
Dim Cy As Single
Dim Cz As Single
Dim i As Integer
' Compute an upward pointing normal to the plane.
target.TransformedNormalVector Nx, Ny, Nz
If Nz < 0 Then
Nx = -Nx
Ny = -Ny
Nz = -Nz
End If
' Get a point on the plane.
target.GetTransformedPoint 1, px, py, pz
' See if the points in this polygon all lie
' above the plane containing target.
For i = 1 To NumPts
' Get the vector from plane to point.
dx = Points(i).trans(1) - px
dy = Points(i).trans(2) - py
dz = Points(i).trans(3) - pz
' If the dot product < 0, the point is
' below the plane.
If dx * Nx + dy * Ny + dz * Nz < -0.01 Then
IsAbove = False
Exit Function
End If
Next i
IsAbove = True
End Function
' Return true if this polygon is completly below
' the plane containing target.
Public Function IsBelow(ByVal target As Face3d) As Boolean
Dim Nx As Single
Dim Ny As Single
Dim Nz As Single
Dim px As Single
Dim py As Single
Dim pz As Single
Dim dx As Single
Dim dy As Single
Dim dz As Single
Dim Cx As Single
Dim Cy As Single
Dim Cz As Single
Dim i As Integer
' Compute a downward pointing normal to the plane.
target.TransformedNormalVector Nx, Ny, Nz
If Nz > 0 Then
Nx = -Nx
Ny = -Ny
Nz = -Nz
End If
' Get a point on the plane.
target.GetTransformedPoint 1, px, py, pz
' See if the points in this polygon all lie
' below the plane containing target.
For i = 1 To NumPts
' Get the vector from plane to point.
dx = Points(i).trans(1) - px
dy = Points(i).trans(2) - py
dz = Points(i).trans(3) - pz
' If the dot product < 0, the point is
' below the plane.
If dx * Nx + dy * Ny + dz * Nz < -0.01 Then
IsBelow = False
Exit Function
End If
Next i
IsBelow = True
End Function
' Return the transformed coordinates of a point
' on the polygon.
Public Sub GetTransformedPoint(ByVal Index As Long, ByRef X As Single, ByRef Y As Single, ByRef Z As Single)
X = Points(Index).trans(1)
Y = Points(Index).trans(2)
Z = Points(Index).trans(3)
End Sub
' Return the bounds of this polygon.
Public Sub GetExtent(ByRef xmin As Single, ByRef xmax As Single, ByRef ymin As Single, ByRef ymax As Single, ByRef zmin As Single, ByRef zmax As Single)
Dim i As Integer
If NumPts < 1 Then Exit Sub
With Points(1)
xmin = .trans(1)
xmax = xmin
ymin = .trans(2)
ymax = ymin
zmin = .trans(3)
zmax = zmin
End With
For i = 2 To NumPts
With Points(i)
If xmin > .trans(1) Then xmin = .trans(1)
If xmax < .trans(1) Then xmax = .trans(1)
If ymin > .trans(2) Then ymin = .trans(2)
If ymax < .trans(2) Then ymax = .trans(2)
If zmin > .trans(3) Then zmin = .trans(3)
If zmax < .trans(3) Then zmax = .trans(3)
End With
Next i
End Sub
' Compute a normal vector for this polygon.
Public Sub NormalVector(ByRef Nx As Single, ByRef Ny As Single, ByRef Nz As Single)
Dim Ax As Single
Dim Ay As Single
Dim Az As Single
Dim Bx As Single
Dim By As Single
Dim Bz As Single
Ax = Points(2).coord(1) - Points(1).coord(1)
Ay = Points(2).coord(2) - Points(1).coord(2)
Az = Points(2).coord(3) - Points(1).coord(3)
Bx = Points(3).coord(1) - Points(2).coord(1)
By = Points(3).coord(2) - Points(2).coord(2)
Bz = Points(3).coord(3) - Points(2).coord(3)
m3Cross Nx, Ny, Nz, Ax, Ay, Az, Bx, By, Bz
End Sub
' Compute a unit normal vector for this polygon.
Public Sub UnitNormalVector(ByRef Nx As Single, ByRef Ny As Single, ByRef Nz As Single)
Dim length As Single
NormalVector Nx, Ny, Nz
length = Sqr(Nx * Nx + Ny * Ny + Nz * Nz)
Nx = Nx / length
Ny = Ny / length
Nz = Nz / length
End Sub
' Return the proper shade for this face
' due to the indicated light source.
Private Function SurfaceColor(ByVal light_sources As Collection, ByVal ambient_light As Integer) As Long
Dim light As LightSource
Dim Lx As Single
Dim Ly As Single
Dim Lz As Single
Dim L_len As Single
Dim Nx As Single
Dim Ny As Single
Dim Nz As Single
Dim NdotL As Single
Dim R As Integer
Dim G As Integer
Dim B As Integer
Dim distance_factor As Single
' Find the unit surface normal. It is the
' same for all the light sources.
UnitNormalVector Nx, Ny, Nz
For Each light In light_sources
' Find the unit vector pointing towards the light.